package org.mmtk.plan.colormark;

import org.mmtk.plan.Trace;
import org.vmmagic.pragma.Uninterruptible;
import org.vmmagic.unboxed.ObjectReference;

@Uninterruptible
public class CMHelper {
	private ObjectReference color;
	private long roots;
	private long traced;
	private long madeLive;
	private long colored;
	private boolean live;
	private Colors colors;
	private Trace trace;
	private CMHelperTraceLocal localTrace;
	
	public CMHelper(int size) {
		this.color = ObjectReference.fromObject(this);
		this.colors = new Colors(size - 1);
		this.trace = new Trace(CM.metaDataSpace);
		this.localTrace = new CMHelperTraceLocal(this);	
		
		prepare();
	}
	
	public void prepare() {
		this.roots = 0;
		this.traced = 0;
		this.madeLive = 0;
		this.colored = 0;
		this.live = false;
		this.colors.init();
		this.trace.prepare();
		this.localTrace.prepare();
	}
	
	public void addRoot() {
		this.roots++;
	}
	
	public long getRoots() {
		return this.roots;
	}
	
	public void help() {
		this.localTrace.helperTrace();
	}
	
	public void setLive() {
		this.live = true;
	}
	
	public long getTraced() {
		return this.traced;
	}
	
	public void addTraced() {
		this.traced++;
	}

	public ObjectReference getColor() {
		return this.color;
	}
	
	public boolean markLive() {
		if(live == true) {
			return false;
		}
		live = true;
		return true;
	}

	public Trace getTrace() {
		return this.trace;
	}
	
	public void addColor(ObjectReference color) {
		this.colors.addColor(color);
	}
	
	public boolean isLive() {
		return live;
	}
	
	public void addLive() {
		this.madeLive++;
	}
	
	public long getLive() {
		return this.madeLive;
	}
	
	public void addColored() {
		this.colored++;
	}
	
	public long getColored() {
		return this.colored;
	}
	
	public boolean hasColor(ObjectReference color) {
		return this.colors.hasColor(color);	
	}
	
	public Colors getDependentColors() {
		return this.colors;
	}
}
